import Config from '@/common/config.js'
import Common from '@/common/common.js'

// 时间过滤器
let formatTime = time => {
	if (time) {
		var a = new Date(time * 1000).toJSON();
		var date = new Date(+new Date(a) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
			'')
		return date
	}
}
// 日期过滤器
let formatDate = (timestamp, type = 0) => {
	var time = new Date(timestamp * 1000);
	var year = time.getFullYear();
	var month = time.getMonth() + 1;
	var date = time.getDate();
	var hour = time.getHours();
	var minute = time.getMinutes();
	var second = time.getSeconds();
	month = month < 10 ? "0" + month : month;
	date = date < 10 ? "0" + date : date;
	hour = hour < 10 ? "0" + hour : hour;
	minute = minute < 10 ? "0" + minute : minute;
	second = second < 10 ? "0" + second : second;
	var arr = [
		year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
		year + "-" + month + "-" + date + " " + hour + ":" + minute,
		year + "年" + month + "月" + date + "日",
		hour + ":" + minute + ":" + second
	]
	return arr[type]
}

// 日期过滤器
let parseTime = (time, pattern) => {
	if (arguments.length === 0 || !time) {
		return null
	}
	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		} else if (typeof time === 'string') {
			time = time.replace(new RegExp(/-/gm), '/');
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		if ((typeof time === 'number') && (time.toString().length >= 5) && (time.toString().length <= 9)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		// Note: getDay() returns 0 on Sunday
		if (key === 'a') {
			return ['日', '一', '二', '三', '四', '五', '六'][value]
		}
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}

// 加入api地址
let formatImgUrl = (url,type = 1) => {
	var reg = new RegExp("http");
	
	if (!isNotEmpty(url)) {
		if(type == 1){
			// return Config.CdnUrl + '/assets/images/default_avatar.png'
			return '/static/images/logo12.png'
		}else{
			return Config.CdnUrl + ''
		}
	} else {
		if(url.substr(1,6) == 'images'){
		
			return "/static"+url
		}
		if (url.indexOf('http') < 0 && url.indexOf('@/') < 0 && url.indexOf('./') < 0 && url.indexOf('base64,') <
			0) {
			if (url.slice(0, 1) == '/') {
				url = url.slice(1, url.length)
			}
			return Config.CdnUrl + url
		} else {
			return url
		}
	}
}

let formatImgUrlNew = (url,type = 1) => {
	var reg = new RegExp("http");
	
	if (!isNotEmpty(url)) {
		if(type == 1){
			// return Config.CdnUrl + '/assets/images/default_avatar.png'
			return '/static/images/logo12.png'
		}else{
			return Config.CdnUrl + ''
		}
	} else {
		if(url.substr(1,6) == 'images'){
		
			return "/static"+url
		}
		if (url.indexOf('http') < 0 && url.indexOf('@/') < 0 && url.indexOf('./') < 0 && url.indexOf('base64,') <
			0) {
			if (url.slice(0, 1) == '/') {
				url = url.slice(1, url.length)
			}
			return 'https://cdnauth.dreamyuewen.com' + url
		} else {
			return url
		}
	}
}

// 富文本图片自适应
let filterRichText = richText => {
	if (richText) {
		const images = richText.match(/<img.*?>/g);
		if (images) {
			images.forEach((image) => {
				const imgSrc = image.match(/src="([^"]*)"/)[1];

				const newImage =
					'<img mode="widthFix" width="100%" style="vertical-align: bottom; display: flex; justify-content: center; align-items: center;" src="' +
					formatImgUrl(imgSrc) + '"/>'

				richText = richText.replace(image, newImage);
			});
		}
		richText = richText.replace(/\n/g, '<br>');
		return richText
	}
}

let isNotEmpty = obj => {
	if (typeof obj == undefined || obj == null || obj == "" || obj == "undefined" || obj.length == 0) {
		return false;
	} else {
		return true;
	}
}

let makeNickName = userID => {
	return userID === 'administrator' ? '系统管理员' : maskUsername('约文用户' + userID)
}

let maskUsername = username => {
	if (username.length <= 2) {
		return username;
	}
	const firstChar = username.charAt(0);
	const lastChar = username.charAt(username.length - 1);
	const middleChars = '*'.repeat(username.length - 2);
	return firstChar + '**' + lastChar;
}

let objTimeDate = time => {
	if (typeof time == 'string') {
		time = time.split(',')
	}
	var date = ''
	time.forEach((item, index) => {
		if (index) {
			date += '-'
		}
		date += parseTime(item, `{y} {m}.{d}`)
	})
	return date
}

let stringSlice = (string, index) => {
	return string.slice(index, string.length)
}

// 正则匹配http
let filterHttp = (url) => {
	if (url.startsWith("http://") || url.startsWith("https://")) {
		return url;
	} else {
		return "http://" + url;
	}
}

export {
	formatTime,
	formatDate,
	parseTime,
	objTimeDate,
	formatImgUrl,
	formatImgUrlNew,
	filterRichText,
	isNotEmpty,
	makeNickName,
	maskUsername,
	stringSlice,
	filterHttp
};